<!--历史数据-->
<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="开始时间:">
              <a-date-picker v-model:value="where.searchBeginTime" type="date" show-time format="YYYY-MM-DD HH:mm:ss"
                             valueFormat="YYYY-MM-DD HH:mm:ss"/>
            </a-form-item>
            <a-form-item label="结束时间:">
              <a-date-picker v-model:value="where.searchEndTime" type="date" show-time format="YYYY-MM-DD HH:mm:ss"
                             valueFormat="YYYY-MM-DD HH:mm:ss"/>
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>

      <a-card
        :bordered="false"
        :title="labelContent"
        :body-style="{padding: '16px 6px 0 0'}">

        <baidu-map
          ak="OsoBFa0ZqPGS0UpqiiF0rVHsBp0kfQ5F"
          :center="pathCenter"
          :zoom="12" style="height: 100vh;">
          <bm-polyline :path="paths"
                       :strokeWeight="3"
                       :strokeOpacity="0"
          > </bm-polyline>
        </baidu-map>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="chdId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          v-model:selection="selection"
          :scroll="{ x: 'max-content' }"
        >
          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>

  </div>
</template>

<script>
import { BaiduMap, BmPolyline } from 'vue-baidu-map-3x'
import {message} from "ant-design-vue";
import moment from "moment";
import {DeviceCattleFootpositionDataApi} from "@/api/property/cattlesports/DeviceCattleFootpositionDataApi";

export default {
  name: 'CattleManagerCattlesportFootpositionData',
  components: {
    BaiduMap,
    BmPolyline
  },
  props: {
    data: Object
  },
  mounted() {
    this.getFootPositoinData();
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '时间',
          dataIndex: 'uploadTime'
        },
        {
          title: '当前纬度',
          dataIndex: 'latitude'
        },
        {
          title: '当前经度',
          dataIndex: 'longitude'
        },
        {
          title: '运动量',
          dataIndex: 'step'
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {
        "cattleId": this.data.cattleId
      },
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showDetail: false,
      paths : [],
      labelContent : '运动轨迹',
      pathCenter : {lng: 116.404, lat: 39.915}
    }
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2022/03/05 14:48
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({page: 1});
      this.getFootPositoinData();
    }
    ,

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2022/03/05 14:48
     */
    reset() {
      this.where.cattleId = this.data.cattleId;
      this.where.searchBeginTime = '';
      this.where.searchEndTime = '';
      this.$nextTick(() => {
        this.reload();
      });
    }
    ,

    /**
     * 删除
     *
     * @author cancan
     * @date 2022/03/05 14:48
     */
    async remove(row) {
      const result = await DeviceCattleFootpositionDataApi.delete({chdId: row.chdId});
      message.success(result.message);
      this.reload();
    },

    getFootPositoinData() {
      let that = this;
      let params = {
        searchBeginTime : moment().subtract(1, 'h').format('YYYY-MM-DD HH:mm:ss'),
        searchEndTime : moment().format('YYYY-MM-DD HH:mm:ss'),
        cattleId: this.data.cattleId,
        orderBy : "upload_time",
        sortBy : "asc"
      };

      if (this.where.searchBeginTime && this.where.searchEndTime) {
        params.searchBeginTime = this.where.searchBeginTime;
        params.searchEndTime = this.where.searchEndTime;
      }

      DeviceCattleFootpositionDataApi.list(params).then(list => {
        let _paths = [];
        let _totalStep = 0;
        let _last = null;
        list.forEach(function (item, index) {
          let formatItem = {
            lng: item.longitude,
            lat: item.latitude
          }
          if (index == 0) {
            that.pathCenter = formatItem;
          }
          _totalStep = _totalStep + item.step;
          _paths.push(formatItem);
          _last = formatItem;
        });
        console.log(_paths);
        that.paths = _paths;
        if (_last) {
          that.labelContent  = "运动轨迹-当前时间段内总步数是：" + _totalStep;
        } else {
          that.labelContent  = "运动轨迹";
        }
      });
    },


    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({page, limit, where, orders}) {
      return DeviceCattleFootpositionDataApi.findPage({...where, ...orders, pageNo: page, pageSize: limit});
    }

  }
};
</script>
